<template>
  <div class="my-container">
    <!-- 已登录 -->
       <div v-if="user" class="header user-info">
      <div class="base-info">
        <div class="left">
          <van-image round
              class="avatar"
              :src="userInfo.photo" fit="cover"
          />
          <span class="name">个人头条号</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 未登录 -->
    <div v-else class="header not-login">
      <div class="login-png" @click="$router.push('/login')">
        <img class="mobile-png" src="@/assets/mobile.png" alt="">
        <span class="text" >登录/注册</span>
      </div>
    </div>

    <!-- 导航宫格 -->
    <van-grid class="grid-nav" column-num="2" clickable>
      <van-grid-item class="grid-item" >
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item"  >
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>

    <van-cell title="消息通知" is-link />
    <van-cell class="md-9" title="小智同学" is-link  />
    <van-cell v-if="user" class="logout-cell" title="退出登录" @click="onlogout" clickable/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'MyIndex',
  data () {
    return {
      userInfo: {} // 用户信息
    }
  },
  created () {
    if (this.user) {
      // 如果用户登录则请求加载用户信息
      this.loadUserInfo()
    }
  },
  methods: {
    onlogout () {
      // 1.退出提示
      // 在组件中要用this.$dialog来调用弹窗
      this.$dialog.confirm({
        title: '确认退出吗？'
      })
        .then(() => {
          // on confirm
          // 成功执行
          // 2.退出清除user和本地的token
          // vuex里面的setuser就是管理user的值的，所以直接传一个空值即可
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // 失败执行
          // on cancel
        })
    },
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        this.userInfo = data.data
      } catch (error) {
        this.$toast('获取数据失败，请稍后再试')
      }
    }

  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 175px;
   background: url('@/assets/banner.png');
   background-size: cover;

  }
  .not-login {
      display: flex;
      justify-content: center;
      align-items: center;
      .login-png {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      .mobile-png {
        width: 68px;
        height: 68px;
      }
      .text {
        padding-top: 10px;
        font-size: 20x;
        color: black;
      }
      }
  }
  .user-info {
    .base-info {
      height: 80px;

      box-sizing: border-box;
      padding: 20px 16px 15px;
      display: flex;
      justify-content: space-between;
      align-content: center;
      .left {
        display: flex;
        align-content: center;
        .avatar {
          width: 66px;
          height: 66px;
          margin-right: 23px;
          border: 1px solid #fff;
        }
        .name {
          font-size: 15px;
          color: #fff;

        }
      }
    }
    .data-stats {

      display: flex;
      .data-item {
        padding:  20px;
        height: 75px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-content: center;
        color: #fff;
        .count {
          margin-bottom: 10px;
          text-align: center;
          font-size: 20px;
        }
        .text {
          text-align: center;
          font-size: 20px;
        }
      }
    }
  }
  .grid-nav {
    height: 100px;
    .grid-item {
      i.toutiao {
        font-size: 30px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 15px;
      }
    }
  }
  .logout-cell {
    background-color: aliceblue;
    text-align: center;
    color: orange;
  }
}
</style>
